package com.demo.controller;

import com.demo.config.RedissonSpringDataConfig;
import com.demo.entity.Book;
import com.demo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 注意Redis库当中，key是不能相同的，如果相同会报错---WRONGTYPE Operation against a key holding the wrong kind of value
 */
@Slf4j
@RestController
@RequestMapping("/api/redis")
public class RedisController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/string")
    public String stringValue() {
        stringRedisTemplate.opsForValue().set("name", "张三");
        return stringRedisTemplate.opsForValue().get("name");
    }

    @GetMapping("/redisList")
    public String redisList() {
        String key = "list";
        stringRedisTemplate.opsForList().leftPush(key, "张三");
        stringRedisTemplate.opsForList().leftPush(key, "里斯");
        stringRedisTemplate.opsForList().leftPush(key, "往往");
        return stringRedisTemplate.opsForList().rightPop(key);
    }

    @GetMapping("/redisSet")
    //hashSet---可除重
    public String redisSet() {
        String key = "set";
        stringRedisTemplate.opsForSet().add(key, "张三");
        stringRedisTemplate.opsForSet().add(key, "里斯");
        stringRedisTemplate.opsForSet().add(key, "往往");
        stringRedisTemplate.opsForSet().add(key, "往往");
        stringRedisTemplate.opsForSet().add(key, "里斯");
        return stringRedisTemplate.opsForSet().pop(key);
    }

    @GetMapping("/redisHash")
    //redisHash---相当于Map
    public String redisHash() {
        String key = "hash";
        stringRedisTemplate.opsForHash().put(key, "name", "zhangsan");
        stringRedisTemplate.opsForHash().put(key, "name", "lisi");
        stringRedisTemplate.opsForHash().put(key, "age", "24");
        return String.valueOf(stringRedisTemplate.opsForHash().get(key, "name"));
    }

    @GetMapping("/redisTemplateString")
    public String redisTemplateString() {
        redisTemplate.opsForValue().set("name111", "zhangsan");
        return String.valueOf(redisTemplate.opsForValue().get("name111"));
    }

    @GetMapping("/redisObject")
    public String redisObject() {
        User user = new User();
        user.setAge(1);
        user.setName("古踏仙");
        user.setGender("男");
        redisTemplate.opsForValue().set("user", user);
        return String.valueOf(redisTemplate.opsForValue().get("user"));
    }

    @GetMapping("/redisObject1")
    public String redisObject1() {
        Book book = new Book();
        book.setPrice(1.1);
        book.setAuthor("姑姑");
        book.setId("2020");
        redisTemplate.opsForValue().set("book", book);
        return String.valueOf(redisTemplate.opsForValue().get("book"));
    }

}
